/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import dao.PaisDao;
import entity.Pais;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Alumno
 */
@WebServlet(name = "PaisServlet", urlPatterns = {"/Listar", "/Registrar", "/Actualizar", "/Eliminar"})
public class PaisServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        if (path.equals("/Listar")) {
            listaPaises(request, response);
        } else if (path.equals("/Registrar")) {
            registrarPais(request, response);
        } else if (path.equals("/Actualizar")) {
            actualizarPais(request, response);
        } else if (path.equals("/Eliminar")) {
            eliminarPais(request, response);
        }
    }

    private void listaPaises(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        throw new UnsupportedOperationException("Not yet implemented");
        try {
            PaisDao obj = new PaisDao();
            List<Pais> lista = obj.listar();
            request.setAttribute("lista", lista);
        } catch (Exception e) {
            request.setAttribute("error", e.getMessage());
        }
        RequestDispatcher rd = request.getRequestDispatcher("ListarPaises.jsp");
        rd.forward(request, response);
    }

    private void registrarPais(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Pais p = new Pais();
            p.setIdPais(request.getParameter("txtIdPais"));
            p.setNombrePais(request.getParameter("txtNombrePais"));
            p.setIdRegion(Integer.parseInt(request.getParameter("txtIdRegion")));
            PaisDao obj = new PaisDao();
            int x = obj.grabar(p);
            if (0 < x) {
                request.setAttribute("msg", "Pais registrado con éxito !!!");
            } else {
                request.setAttribute("msg", "Pais no registrado...");
            }
        } catch (Exception e) {
            request.setAttribute("error", e.getMessage());
        }
        RequestDispatcher rd = request.getRequestDispatcher("registrarPais.jsp");
        rd.forward(request, response);
    }

    private void actualizarPais(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Pais p = new Pais();
            p.setIdPais(request.getParameter("txtIdPais"));
            p.setNombrePais(request.getParameter("txtNombrePais"));
            p.setIdRegion(Integer.parseInt(request.getParameter("txtIdRegion")));
            PaisDao obj = new PaisDao();
            int x = obj.modificar(p);
            if (0 < x) {
                request.setAttribute("msg", "Pais actualizado con éxito !!!");
            } else {
                request.setAttribute("msg", "Pais no actualizado...");
            }
        } catch (Exception e) {
            request.setAttribute("error", e.getMessage());
        }
        RequestDispatcher rd = request.getRequestDispatcher("modificarPais.jsp");
        rd.forward(request, response);
    }

    private void eliminarPais(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Pais p = new Pais();
            p.setIdPais(request.getParameter("txtIdPais"));
            PaisDao obj = new PaisDao();
            int x = obj.eliminar(p);
            if (0 < x) {
                request.setAttribute("msg", "Pais eliminado con éxito !!!");
            } else {
                request.setAttribute("msg", "Pais no eliminado...");
            }
        } catch (Exception e) {
            request.setAttribute("error", e.getMessage());
        }
        RequestDispatcher rd = request.getRequestDispatcher("eliminarPais.jsp");
        rd.forward(request, response);
    }
}
